/*
 * @Description: 检测任务管理操作
 * @Author: zzt
 * @Date: 2022-04-28 13:47:59
 * @LastEditTime: 2023-01-03 17:05:14
 */

import { useMemo, useState } from 'react';
import { Button, Divider, Space } from 'antd';
import cs from 'classnames';
import { useBusinessGuidanceContext } from '../contexts/BusinessGuidanceContext';
import CreateBusinessDialog from './dialogs/CreateBusiness';
import UpLoadBusiness from './dialogs/UpLoadBusiness';
import styles from '../styles/pageOperate.module.scss';
import classNames from 'classnames/bind';
import { useGuidanceTypeContext } from '../contexts/GuidanceWaysContext';

const cx = classNames.bind(styles);

const PageOperate = (props: any) => {
  const { classname } = props;
  const { type, title } = useGuidanceTypeContext();
  const { selectItems, deleteItem, getEnclosure, enclosure } = useBusinessGuidanceContext();

  // 新增弹窗
  const [createVisible, setCreateVisible] = useState(false);
  // 执行检测弹窗
  const [upDataVisible, setUpDataVisible] = useState(false);
  const onChange = () => {
    getEnclosure(selectItems[0], type);
    setUpDataVisible(true);
  };
  const onPerform = () => {
    deleteItem(selectItems, `您已选择了${selectItems.length}个业务指导，是否确认移除?`, type);
  };
  return (
    <div className={cs('filter-container', 'page-operate', classname)}>
      <div>
        {title}
        {type === 1 && (
          <Button
            // onClick={() => {
            //   setCreateVisible(true);
            // }}
            type="primary"
            className={cx('butLeft')}
          >
            进入该单位进行检查
          </Button>
        )}
      </div>
      {type !== 3 && (
        <Space size="small">
          <Button
            onClick={() => {
              setCreateVisible(true);
            }}
          >
            新增
          </Button>

          {selectItems.length === 1 ? (
            <Button
              onClick={() => {
                onChange();
              }}
            >
              修改
            </Button>
          ) : (
            <Button disabled>修改</Button>
          )}
          {selectItems.length > 0 ? (
            <Button onClick={onPerform}>删除</Button>
          ) : (
            <Button disabled>删除</Button>
          )}
          <Divider type="vertical" />
          <Button>筛选检索</Button>
        </Space>
      )}

      {useMemo(
        () => (
          <CreateBusinessDialog
            visible={createVisible}
            onClose={() => {
              setCreateVisible(false);
            }}
          />
        ),
        [createVisible]
      )}
      {useMemo(
        () => (
          <UpLoadBusiness
            visible={upDataVisible}
            data={enclosure}
            onClose={() => {
              setUpDataVisible(false);
            }}
          />
        ),
        [upDataVisible, enclosure]
      )}
    </div>
  );
};

export default PageOperate;
